package com.bw.im.msg

import androidx.lifecycle.viewModelScope
import com.bw.common.mvi.BaseViewModel
import com.bw.im.msg.entity.MsgResponseEntity
import com.bw.im.msg.entity.MsgState
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class MsgViewModel : BaseViewModel<MsgRepo,MsgState>() {

    private var page = 1

    override fun createRepo(): MsgRepo {return MsgRepo() }

    override fun createState(): MsgState { return MsgState(false,false,ArrayList()) }

    fun requestRefresh(){
        page =1
        val map = HashMap<String,Any>()
        map.put("userId",1)
        map.put("page",page)
        viewModelScope.launch {
            subFlow(repo.refreshMsg(map))
                .collect{
                    val entity : MsgResponseEntity = it as MsgResponseEntity
                    mutableContainer.updateUiState {
                        copy(refreshState = true,loadState = false,datas = entity.data) }
                }
        }
    }
    fun requestLoad(){
        page +=1
        val map = HashMap<String,Any>()
        map.put("userId",1)
        map.put("page",page)
        viewModelScope.launch {
            subFlow(repo.loadMsg(map))
                .collect{
                    val entity : MsgResponseEntity = it as MsgResponseEntity
                    mutableContainer.updateUiState {
                        copy(refreshState = false,loadState = true,datas = entity.data) }
                }
        }
    }
}